/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.system;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.system.SystemGroupAccess;
import model.system.SystemMenu;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class GroupAccessTM extends AbstractTableModel{
     
    static protected String[] columnNames = { "NO", "MODUL", "MENU NAME","TAMBAH","EDIT","HAPUS","PRINT","ID" };
    static protected Class<?>[] columnTypes = { Integer.class, String.class, String.class, Boolean.class,Boolean.class,Boolean.class,Boolean.class,Integer.class };
    private List<SystemGroupAccess> data;
    private List<SystemMenu> dataMenu;

    public GroupAccessTM() {
        this(new ArrayList<SystemGroupAccess>(0));
    }

    public GroupAccessTM(List<SystemGroupAccess> data) {
        this.data = data;
    }
    
   public SystemGroupAccess get(int index) {
        return data.get(index);
    }

    public void add(SystemGroupAccess product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, SystemGroupAccess product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<SystemGroupAccess> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
     public void add(SystemMenu product) {
        dataMenu.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }
     
     public void addMenu(Collection<SystemMenu> products) {
        dataMenu.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public int getRowCount() {
        return dataMenu.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getSystemMenu().getMenuGroup();
            case 2: return data.get(rowIndex).getSystemMenu().getMenuName();
            case 3: return Boolean.TRUE;//gotPolicy("ADD;", data.get(rowIndex).getSystemMenu().getPolicy(), true);
            case 4: return gotPolicy("EDIT;", data.get(rowIndex).getSystemMenu().getPolicy(), true);
            case 5: return gotPolicy("DELETE;", data.get(rowIndex).getSystemMenu().getPolicy(), true);
            case 6: return gotPolicy("PRINT;", data.get(rowIndex).getSystemMenu().getPolicy(), true);            
            case 7: return data.get(rowIndex).getSystemMenu().getMenuId();            
            default: return null;
        }
    }
    
    public Boolean gotPolicy(String policyName,String menuPolicy, boolean key){
        Boolean rs = null;
        if (menuPolicy.indexOf(policyName)>-1){
            rs = Boolean.valueOf(key);
        }
        return Boolean.TRUE;
    }
    
     @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
     
     @Override
    public Class getColumnClass(int c) {
         return columnTypes[c];
            //return getValueAt(0, c).getClass();
//        Object value=this.getValueAt(0,c);  
//        if (c==3)
//            return Boolean.class;
//        return (value==null?Object.class:value.getClass());  
    }
     
      @Override
    public boolean isCellEditable(int row, int column) {
        return true;//(column==3 || column==4 || column==5 || column==6);
    }
}
